//
//  YHQController.m
//  ShopDemo
//
//  Created by Sunshine on 2018/7/18.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "YHQController.h"

@interface YHQController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableview;
@property(nonatomic,strong)NSArray *datasource;


@end

@implementation YHQController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LineBackColor;
    NSString *strurl = [NSString stringWithFormat:@"/dashop/usercoupon/app/list.do?USER_ID=%@&COUPON_STATE=0",[UserDefault objectForKey:LogUserID]];
    [self showDataMethodWithUrl:strurl];
    
    [self creatHeaderButtonMethod];
    [self creatTableviewMethod];
    
    
}

//限时抢购按钮图
-(void)showDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:[MethodClass returnUrlSetStrWSDLURL:url] refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        self.datasource = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}

-(void)creatHeaderButtonMethod{
    NSArray *arr = @[@"未使用",@"已使用",@"已过期"];
    for (int i = 0; i < 3; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = LineBackColor;
        btn.tag = 320 + i;
        [btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [self.view addSubview:btn];
        btn.sd_layout
        .topSpaceToView(self.view, 0)
        .leftSpaceToView(self.view, kScreenWidth/3*i)
        .heightIs(45*kJLXWidthScale)
        .widthIs(kScreenWidth/3);
        
        if (i == 0) {
            UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 45*kJLXWidthScale, kScreenWidth/3, 1)];
            self.lineview = lineview;
            lineview.backgroundColor = [UIColor orangeColor];
            [self.view addSubview:lineview];
            self.onebtn = btn;
            [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        }else{
            if (i == 1) {
                self.twobtn = btn;
            }else{
                self.thirdbtn = btn;
            }
             [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    
    UIView *livi = [[UIView alloc]initWithFrame:CGRectMake(0, 46*kJLXWidthScale, kScreenWidth, 1)];
    livi.backgroundColor = LineColor;
    [self.view addSubview:livi];
}

-(void)buttonAction:(UIButton *)sender{
    if (sender.tag - 320 == 0) {
        NSString *strurl = [NSString stringWithFormat:@"/dashop/usercoupon/app/list.do?USER_ID=%@&COUPON_STATE=0",[UserDefault objectForKey:LogUserID]];
        [self showDataMethodWithUrl:strurl];
        [self.onebtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.twobtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.thirdbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.lineview.frame = CGRectMake(0, 45*kJLXWidthScale, kScreenWidth/3, 1);
        }];
        
    }else if (sender.tag - 320 == 1){
        NSString *strurl = [NSString stringWithFormat:@"/dashop/usercoupon/app/list.do?USER_ID=%@&COUPON_STATE=1",[UserDefault objectForKey:LogUserID]];
        [self showDataMethodWithUrl:strurl];
        [self.twobtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.onebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.thirdbtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.lineview.frame = CGRectMake(kScreenWidth/3, 45*kJLXWidthScale, kScreenWidth/3, 1);
        }];
    }else{
        NSString *strurl = [NSString stringWithFormat:@"/dashop/usercoupon/app/list.do?USER_ID=%@&COUPON_STATE=-1",[UserDefault objectForKey:LogUserID]];
        [self showDataMethodWithUrl:strurl];
        [self.thirdbtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.onebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.twobtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [UIView animateWithDuration:0.3 animations:^{
            self.lineview.frame = CGRectMake(kScreenWidth*2/3, 45*kJLXWidthScale, kScreenWidth/3, 1);
        }];
    }
}

-(void)creatTableviewMethod{

    self.listTableview.sd_layout
    .topSpaceToView(self.view, 47*kJLXWidthScale);
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
}

#pragma mark --UITableViewDelegate,UITableViewDataSource--


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    NSDictionary *dic = self.datasource[indexPath.row];
    UIImageView *imagevi = [[UIImageView alloc]initWithFrame:CGRectMake(30*kJLXWidthScale, 10*kJLXWidthScale, kScreenWidth - 60*kJLXWidthScale, 80*kJLXWidthScale)];
    imagevi.backgroundColor = [UIColor redColor];
    UILabel *lab01 = [UILabel new];
    lab01.text = [NSString stringWithFormat:@"%.2f",[dic[@"COUPON_PRICE"] floatValue] ];
    NSInteger numwidth = lab01.text.length *25;
    lab01.font = [UIFont systemFontOfSize:40*kJLXWidthScale weight:0.2];
    lab01.textColor = [UIColor orangeColor];
    [imagevi addSubview:lab01];
    lab01.sd_layout
    .topSpaceToView(imagevi, 5*kJLXWidthScale)
    .leftSpaceToView(imagevi, 5*kJLXWidthScale)
    .heightIs(40*kJLXWidthScale)
    .widthIs(numwidth);
    
    UILabel *lab02 = [UILabel new];
    lab02.text = [NSString stringWithFormat:@"%@",dic[@"COUPON_NAME"]];
    lab02.font = [UIFont systemFontOfSize:18*kJLXWidthScale weight:0.2];
    lab02.textColor = [UIColor orangeColor];
    [imagevi addSubview:lab02];
    lab02.sd_layout
    .topSpaceToView(imagevi,15*kJLXWidthScale)
    .leftSpaceToView(lab01, 5*kJLXWidthScale)
    .heightIs(30*kJLXWidthScale)
    .widthIs(120*kJLXWidthScale);
    
    UILabel *lab03 = [UILabel new];
    lab03.text = [NSString stringWithFormat:@"使用时间%@ - %@",dic[@"STARTTIME"],dic[@"ENDTIME"]];
    lab03.textAlignment = NSTextAlignmentRight;
    lab03.font = [UIFont systemFontOfSize:13*kJLXWidthScale];
    lab03.textColor = [UIColor blackColor];
    [imagevi addSubview:lab03];
    lab03.sd_layout
    .bottomSpaceToView(imagevi,0)
    .rightSpaceToView(imagevi, 8*kJLXWidthScale)
    .heightIs(20*kJLXWidthScale)
    .leftSpaceToView(imagevi, 10*kJLXWidthScale);
    
    
    UILabel *lab04 = [UILabel new];
    lab04.text = [NSString stringWithFormat:@"满%@元减%@元",dic[@"USE_PRICE"],dic[@"COUPON_PRICE"]];
    lab04.textAlignment = NSTextAlignmentRight;
    lab04.font = [UIFont systemFontOfSize:12*kJLXWidthScale];
    lab04.textColor = [UIColor blackColor];
    [imagevi addSubview:lab04];
    lab04.sd_layout
    .bottomSpaceToView(lab03,0)
    .rightSpaceToView(imagevi, 8*kJLXWidthScale)
    .heightIs(15*kJLXWidthScale)
    .leftSpaceToView(imagevi, 10*kJLXWidthScale);
    
    [cell.contentView addSubview:imagevi];
    
    
    
    return cell;
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}



@end
